package com.websem.client.views;

import java.util.HashMap;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.websem.client.services.FilmService;
import com.websem.client.services.FilmServiceAsync;

public class SearchBarView extends Composite {

	private static SearchBarViewUiBinder uiBinder = GWT
			.create(SearchBarViewUiBinder.class);

	/**
	 * Service
	 */
	private final FilmServiceAsync filmService = GWT.create(FilmService.class);

	public static final String releaseYearList[] = { "1997", "1998", "1999",
			"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007",
			"2008", "2009", "2010", "2011", "2012", "2013", "2014" };

	interface SearchBarViewUiBinder extends UiBinder<Widget, SearchBarView> {
	}

	@UiField
	ListBox countryListBox;

	/**
	 * @return the countryListBox
	 */
	public ListBox getCountryListBox() {
		return countryListBox;
	}

	/**
	 * @param countryListBox
	 *            the countryListBox to set
	 */
	public void setCountryListBox(ListBox countryListBox) {
		this.countryListBox = countryListBox;
	}

	@UiField
	ListBox languageListBox;

	/**
	 * @return the languageListBox
	 */
	public ListBox getLanguageListBox() {
		return languageListBox;
	}

	/**
	 * @param languageListBox
	 *            the languageListBox to set
	 */
	public void setLanguageListBox(ListBox languageListBox) {
		this.languageListBox = languageListBox;
	}

	@UiField
	ListBox yearListBox;

	/**
	 * @return the yearListBox
	 */
	public ListBox getYearListBox() {
		return yearListBox;
	}

	/**
	 * @param yearListBox
	 *            the yearListBox to set
	 */
	public void setYearListBox(ListBox yearListBox) {
		this.yearListBox = yearListBox;
	}

	@UiField
	ListBox genreListBox;

	/**
	 * @return the genreListBox
	 */
	public ListBox getGenreListBox() {
		return genreListBox;
	}

	/**
	 * @param genreListBox
	 *            the genreListBox to set
	 */
	public void setGenreListBox(ListBox genreListBox) {
		this.genreListBox = genreListBox;
	}

	@UiField
	ListBox versionListBox;

	/**
	 * @return the versionListBox
	 */
	public ListBox getVersionListBox() {
		return versionListBox;
	}

	/**
	 * @param versionListBox
	 *            the versionListBox to set
	 */
	public void setVersionListBox(ListBox versionListBox) {
		this.versionListBox = versionListBox;
	}

	@UiField
	ListBox prizeListBox;

	/**
	 * @return the prizeListBox
	 */
	public ListBox getPrizeListBox() {
		return prizeListBox;
	}

	/**
	 * @param prizeListBox
	 *            the prizeListBox to set
	 */
	public void setPrizeListBox(ListBox prizeListBox) {
		this.prizeListBox = prizeListBox;
	}

	@UiField
	HorizontalPanel searchBar;

	public SearchBarView() {
		initWidget(uiBinder.createAndBindUi(this));
		searchBar.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		yearListBox.addItem("Date");
		genreListBox.addItem("Genre");
		languageListBox.addItem("Langue");
		countryListBox.addItem("Pays");
		versionListBox.addItem("Version");
		prizeListBox.addItem("Oscar");
		this.initialiseWidgets();
		for (String s : releaseYearList)
			yearListBox.addItem(s);

	}

	private void initialiseWidgets() {
		filmService.getCountry(new AsyncCallback<List<String>>() {

			@Override
			public void onSuccess(List<String> result) {
				for (String str : result) {
					countryListBox.addItem(str);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("COUNTRY -- Callback failed");
			}
		});

		filmService.getGenre(new AsyncCallback<List<String>>() {

			@Override
			public void onSuccess(List<String> result) {
				for (String str : result) {
					genreListBox.addItem(str);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("GENRE -- Callback failed");
			}
		});

		filmService.getLanguage(new AsyncCallback<List<String>>() {

			@Override
			public void onSuccess(List<String> result) {
				for (String str : result) {
					languageListBox.addItem(str);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("LANGUAGE -- Callback failed");
			}
		});

		filmService.getVersion(new AsyncCallback<List<String>>() {

			@Override
			public void onSuccess(List<String> result) {
				for (String str : result) {
					versionListBox.addItem(str);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("VERSION -- Callback failed");
			}
		});

		filmService.getPrizes(new AsyncCallback<List<String>>() {

			@Override
			public void onSuccess(List<String> result) {
				for (String str : result) {
					prizeListBox.addItem(str);
				}

			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("PRIZE -- Callback failed");

			}
		});
	}

	public String getSelectedCriterias() {
		Integer selectedGenre = this.genreListBox.getSelectedIndex();
		Integer selectedCountry = this.countryListBox.getSelectedIndex();
		Integer selectedOscar = this.prizeListBox.getSelectedIndex();
		Integer selectedLanguage = this.languageListBox.getSelectedIndex();
		Integer selectedDate = this.yearListBox.getSelectedIndex();
		Integer selectedVersion = this.versionListBox.getSelectedIndex();
		String result = "";
		result = selectedGenre.toString() + selectedCountry.toString()
				+ selectedOscar.toString() + selectedLanguage.toString()
				+ selectedDate.toString() + selectedVersion.toString();
		return result;
	}

}
